package junior.db.dao;

import junior.db.dao.base.IBaseId;
import junior.db.dao.jdbc.JdbcDao;
import junior.db.dao.mybatis.base.BaseSelectMapper;
import junior.db.dao.mybatis.type.ResultType;
import junior.db.dao.query.AbstractQuery;
import junior.db.dao.query.EntityQuery;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wubo
 * @date 2024/9/9 9:49
 */
class JdbcSelectMapperImpl<E extends IBaseId> implements BaseSelectMapper<E> {
    protected JdbcDao jdbcDao;
    protected Class<E> entityClass;
    
    JdbcSelectMapperImpl(JdbcDao jdbcDao, Class<E> entityClass) {
        this.jdbcDao = jdbcDao;
        this.entityClass = entityClass;
    }
    
    @Override
    public Long count(AbstractQuery query) {
        return jdbcDao.count(convertToEntityQuery(query));
    }
    
    @Override
    public E get(AbstractQuery query) {
        return jdbcDao.get(convertToEntityQuery(query));
    }
    
    @Override
    public <T> T getForObject(AbstractQuery query, ResultType<T> resultType) {
        return jdbcDao.getForObject(convertToEntityQuery(query), resultType.getType());
    }
    
    @Override
    public Map<String, Object> getForMap(AbstractQuery query) {
        return jdbcDao.getForMap(convertToEntityQuery(query));
    }
    
    @Override
    public String getForString(AbstractQuery query) {
        return jdbcDao.getForString(convertToEntityQuery(query));
    }
    
    @Override
    public Integer getForInteger(AbstractQuery query) {
        return jdbcDao.getForInteger(convertToEntityQuery(query));
    }
    
    @Override
    public Long getForLong(AbstractQuery query) {
        return jdbcDao.getForLong(convertToEntityQuery(query));
    }
    
    @Override
    public Boolean getForBoolean(AbstractQuery query) {
        return jdbcDao.getForBoolean(convertToEntityQuery(query));
    }
    
    @Override
    public BigDecimal getForBigDecimal(AbstractQuery query) {
        return jdbcDao.getForBigDecimal(convertToEntityQuery(query));
    }
    
    @Override
    public Date getForDate(AbstractQuery query) {
        return jdbcDao.getForDate(convertToEntityQuery(query));
    }
    
    @Override
    public LocalDate getForLocalDate(AbstractQuery query) {
        return jdbcDao.getForLocalDate(convertToEntityQuery(query));
    }
    
    @Override
    public LocalDateTime getForLocalDateTime(AbstractQuery query) {
        return jdbcDao.getForLocalDateTime(convertToEntityQuery(query));
    }
    
    @Override
    public List<E> list(AbstractQuery query) {
        return jdbcDao.list(convertToEntityQuery(query));
    }
    
    @Override
    public <T> List<T> listForObject(AbstractQuery query, ResultType<T> resultType) {
        return jdbcDao.listForObject(convertToEntityQuery(query), resultType.getType());
    }
    
    @Override
    public List<Map<String, Object>> listForMap(AbstractQuery query) {
        return jdbcDao.listForMap(convertToEntityQuery(query));
    }
    
    @Override
    public List<String> listForString(AbstractQuery query) {
        return jdbcDao.listForString(convertToEntityQuery(query));
    }
    
    @Override
    public List<Integer> listForInteger(AbstractQuery query) {
        return jdbcDao.listForInteger(convertToEntityQuery(query));
    }
    
    @Override
    public List<Long> listForLong(AbstractQuery query) {
        return jdbcDao.listForLong(convertToEntityQuery(query));
    }
    
    @Override
    public List<Boolean> listForBoolean(AbstractQuery query) {
        return jdbcDao.listForBoolean(convertToEntityQuery(query));
    }
    
    @Override
    public List<BigDecimal> listForBigDecimal(AbstractQuery query) {
        return jdbcDao.listForBigDecimal(convertToEntityQuery(query));
    }
    
    @Override
    public List<Date> listForDate(AbstractQuery query) {
        return jdbcDao.listForDate(convertToEntityQuery(query));
    }
    
    @Override
    public List<LocalDate> listForLocalDate(AbstractQuery query) {
        return jdbcDao.listForLocalDate(convertToEntityQuery(query));
    }
    
    @Override
    public List<LocalDateTime> listForLocalDateTime(AbstractQuery query) {
        return jdbcDao.listForLocalDateTime(convertToEntityQuery(query));
    }
    
    protected EntityQuery convertToEntityQuery(AbstractQuery query) {
        if (query instanceof EntityQuery) {
            return (EntityQuery) query;
        }
        EntityQuery eq = new EntityQuery(entityClass, query.getPrefix());
        eq.fromFilterParam(query);
        return eq;
    }
}
